package dtd.PHS.VND_ExchangeRate.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import dtd.PHS.VND_ExchangeRate.R;
import dtd.PHS.VND_ExchangeRate.data.ListRates;
import dtd.PHS.VND_ExchangeRate.data.RateItem;

public class PreferencesHelpers {

	private static final String PREF_WIDGET_RATE = "Wiget_rate";
	private static final boolean DEFAULT_UPDATE_BY_INTERNET_TURNED_ON = true;
	private static final String DEFAULT_WIDGET_UPDATE_PERIOD = "30";

	public static RateItem getWidgetRateItem(ListRates rates) {
		String widget_rate = Helpers.getPreference(PREF_WIDGET_RATE);
		if ( widget_rate == null) {
			widget_rate = RateItem.MyCurrency.USD.toString();
		}
		for(RateItem rate : rates) {
			if ( rate.getCurrencyName().equals(widget_rate)) {
				return rate;
			}
		}
		return null;
	}

	public static void setWidgetRateItem(String currency) {
		Helpers.setPreference(PREF_WIDGET_RATE, currency);
	}

	public static boolean isTobeUpdatedByInternet() {
		Context context = ApplicationContext.getContext();
		String prefName = context.getString(R.string.PREF_UPDATE_ON_INTERNET);
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
		boolean pref = settings.getBoolean(prefName, true);
		return pref;
	}
	
	public static long getWidgetUpdatePeriod() {
		String prefKey = ApplicationContext.getContext().getString(R.string.PREF_WIDGET_UPDATE_PERIOD);
		String prefVal = Helpers.getPreference(prefKey);
		if ( prefVal == null) {
			Helpers.setPreference(prefKey, DEFAULT_WIDGET_UPDATE_PERIOD);
			return Long.parseLong(DEFAULT_WIDGET_UPDATE_PERIOD) * 60 * 1000;
		} else {
			return Long.parseLong(prefVal) * 60 * 1000;
		}
	}

}
